package com.x.ganjiang.gen.gen;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.x.ganjiang.gen.dao.DbeaverScriptContentIService;
import com.x.ganjiang.gen.dao.DbeaverScriptDirIService;
import com.x.ganjiang.gen.data.DbeaverScriptContentDO;
import com.x.ganjiang.gen.data.DbeaverScriptDirDO;
import com.x.ganjiang.gen.infra.mapper.DbeaverScriptContentMapper;
import com.x.ganjiang.gen.infra.mapper.DbeaverScriptDirMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

//@Component
@Slf4j
public class DbeaverScriptFileGenRunner implements ApplicationRunner {

    @Resource
    private DbeaverScriptDirMapper dbeaverScriptDirMapper;

    @Resource
    private DbeaverScriptDirIService dbeaverScriptDirIService;

    @Resource
    private DbeaverScriptContentMapper dbeaverScriptContentMapper;

    @Resource
    private DbeaverScriptContentIService dbeaverScriptContentIService;


    @Resource
    private ConfigurableApplicationContext ctx;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        try {
            genCode();
        } catch (Exception e) {
            log.error("GenRunner.run error", e);
        } finally {
            int code = SpringApplication.exit(ctx, () -> 0); // 0 表示正常退出
            System.exit(code);
        }
    }

    public static Map<String, String> dirNameMap = new HashMap<>();


    public void preGenCode() {
        // 目录
        LambdaQueryWrapper<DbeaverScriptDirDO> dbeaverScriptDirWrapper = new LambdaQueryWrapper<>();
        dbeaverScriptDirWrapper.eq(DbeaverScriptDirDO::getIsDel, "0");
        List<DbeaverScriptDirDO> dbeaverScriptDirDOList = dbeaverScriptDirIService.list(dbeaverScriptDirWrapper);
        dirNameMap = dbeaverScriptDirDOList.stream()
                .filter(e -> Objects.nonNull(e.getDirName()))
                .collect(Collectors.toMap(DbeaverScriptDirDO::getDirNo, DbeaverScriptDirDO::getDirName));
    }

    public void genCode() {
        preGenCode();
        long begin = System.currentTimeMillis();
        String dirPath = "F:\\code\\per\\x-sql\\dbScripts\\";
        LambdaQueryWrapper<DbeaverScriptContentDO> dbeaverScriptContentWrapper = new LambdaQueryWrapper<>();
        dbeaverScriptContentWrapper.eq(DbeaverScriptContentDO::getIsDel, "0");
        List<DbeaverScriptContentDO> dbeaverScriptContentDOList = dbeaverScriptContentIService.list(dbeaverScriptContentWrapper);
        dbeaverScriptContentDOList.stream()
                .filter(e -> Objects.nonNull(e.getPositionName()) && Objects.nonNull(e.getScriptContent()))
                .forEach(e -> {
                    String dirName = dirNameMap.get(e.getDirNo());
                    String positionName = e.getPositionName();
                    String scriptName = Objects.isNull(e.getScriptName()) ? "" : e.getScriptName();
                    String path = dirPath + dirName.trim() + "\\" + positionName.trim() + "_" + scriptName.trim() + ".sql";
                    log.info("文件路径：{}", path);
                    String scriptContent = "select content_no,position_name ,script_name,script_content ,remark  from `ganjiang-db`.dbeaver_script_content_tbl where position_name = '"+e.getPositionName()+"';\n"
                            + e.getScriptContent();
                    FileUtil.writeUtf8String(scriptContent, path);
                });
        log.info("生成代码耗时ms：{}", System.currentTimeMillis() - begin);
    }


}
